﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the G.O.S.E(Game Online Server Engine) for .NET.
//
//                 2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Demo_G.O.S.E.ServerEngine.Common;
#endregion

namespace Demo_G.O.S.E.ServerEngine.Network.DLL
{
    /// <summary>
    /// 服务端的主处理客户的服务
    /// </summary>
    [MultiThreadedSupportgAttribute( "当前的类所有成员都可锁定,支持多线程操作" )]
    public class ServiceHandleManager
    {
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 当前的主处理的指针
        /// </summary>
        private IntPtr m_Value      = IntPtr.Zero;
        #endregion

        #region zh-CHS 共有属性 | en Public Properties
        /// <summary>
        /// 全部连接过的客户端的数量
        /// </summary>
        public int ClientCount
        {
            get { return ServiceHandleManager.ServiceHandleManager_TotalClients(m_Value); }
        }

        /// <summary>
        /// 在线客户端的数量
        /// </summary>
        public int OnlineClientCount
        {
            get { return ServiceHandleManager.ServiceHandleManager_OnlineClients(m_Value); }
        }

        /// <summary>
        /// 给出某个客户端
        /// </summary>
        /// <param name="strIndex">地址和端口</param>
        /// <returns></returns>
        public ServiceHandle this[string strIndex]
        {
            get
            {
                IntPtr newValue = IntPtr.Zero;

                if ( ServiceHandleManager.ServiceHandleManager_GetClientHandle( strIndex, ref newValue, m_Value ) == false )
                {
                    Debug.WriteLine( "ServiceHandleManager.this[string] - ServiceHandleManager.ServiceHandleManager_GetClientHandle(...) == false error!" );

                    return null;
                }

                if ( newValue == IntPtr.Zero )
                {
                    Debug.WriteLine( "ServiceHandleManager.this[string] - newValue == IntPtr.Zero error!" );

                    return null;
                }

                ServiceHandle clientHandlerAtServer           = new ServiceHandle();
                clientHandlerAtServer.Value                   = newValue;

                clientHandlerAtServer.SocketHandlerManager    = this;

                return clientHandlerAtServer;
            }
        }
        #endregion

        #region zh-CHS 共有方法 | en Public Methods
        /// <summary>
        /// 发送数据到全部的客户端
        /// </summary>
        /// <param name="sendMessageBlock"></param>
        public void SendToAll(MessageBlock sendMessageBlock)
        {
            if ( sendMessageBlock == null )
            {
                Debug.WriteLine( "ServiceHandleManager.ServiceHandleManager_SendToAll(...) - sendMessageBlock == null error!" );

                return;
            }

            if ( ServiceHandleManager.ServiceHandleManager_SendToAll( sendMessageBlock.Value, m_Value ) == false )
            {
                Debug.WriteLine( "ServiceHandleManager.ServiceHandleManager_SendToAll(...) - ServiceHandleManager.ServiceHandleManager_SendToAll(...) == false error!" );

                return;
            }
        }

        /// <summary>
        /// 给出新的数据区
        /// </summary>
        /// <returns></returns>
        public MessageBlock GetNewSendMessageBlock()
        {
            IntPtr newMessageBlockValue = IntPtr.Zero;
            if ( ServiceHandleManager.ServiceHandleManager_GetNewSendMessageBlock( ref newMessageBlockValue, m_Value ) == false )
            {
                Debug.WriteLine( "ServiceHandleManager.GetNewSendMessageBlock(...) - ServiceHandleManager.ServiceHandleManager_GetNewSendMessageBlock(...) == false error!" );

                return null;
            }

            if ( newMessageBlockValue == IntPtr.Zero )
            {
                Debug.WriteLine( "ServiceHandleManager.GetNewSendMessageBlock(...) - newMessageBlockValue == IntPtr.Zero error!" );

                return null;
            }

            MessageBlock newSendMessageBlock = new MessageBlock();
            newSendMessageBlock.Value        = newMessageBlockValue;

            return newSendMessageBlock;
        }
        #endregion

        #region zh-CHS 内部属性 | en Internal Properties
        /// <summary>
        /// 主处理的指针
        /// </summary>
        protected internal IntPtr Value
        {
            get { return m_Value; }
            set { m_Value = value; }
        }
        #endregion

        #region zh-CHS 引入DLL接口 | en DLL Import
        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pSocketHandlerManagerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern int ServiceHandleManager_TotalClients(IntPtr pSocketHandlerManagerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pSocketHandlerManagerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern int ServiceHandleManager_OnlineClients(IntPtr pSocketHandlerManagerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="strIndex"></param>
        /// <param name="pClientHandlerAtServer"></param>
        /// <param name="pSocketHandlerManagerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern bool ServiceHandleManager_GetClientHandle( string strIndex, ref IntPtr pClientHandlerAtServer, IntPtr pSocketHandlerManagerAtServer );

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pMessageBlock"></param>
        /// <param name="pSocketHandlerManagerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern bool ServiceHandleManager_SendToAll(IntPtr pMessageBlock, IntPtr pSocketHandlerManagerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pNewSendMessageBlock"></param>
        /// <param name="pSocketHandlerManagerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern bool ServiceHandleManager_GetNewSendMessageBlock(ref IntPtr pNewSendMessageBlock, IntPtr pSocketHandlerManagerAtServer);
        #endregion
    }
}
#endregion

